home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc B) / Acorn User China CD-ROM (UK) (Disc B).bin / STUTTGART / FROMUTS / UNIXLIB37B / src_c_strtod < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-23  |  1.3 KB  |  72 lines

  1. #ifdef __STDC__
  2. static char sccs_id[] = "@(#) strtod.c 1.3 "__DATE__" HJR";
  3. #else
  4. static char sccs_id[] = "@(#) strtod.c 1.3 26/9/90 HJR";
  5. #endif
  6.  
  7. /* strtod.c (c) Copyright 1990 H.Rogers */
  8.  
  9. #include <ctype.h>
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #else
  13. extern double strtod();
  14. #endif
  15.  
  16. #define MAXEXP    308    /* maximum decimal exponential for IEEE double */
  17.  
  18. /* recognizes: [spaces][sign][digs][[.][digs]][[e|E][space|sign][int]] */
  19.  
  20. /* this is the most efficient strtod() can be, without resorting to
  21.  * assuming IEEE 'D' or some other floating point representation */
  22.  
  23. double strtod(register const char *s,char **end)
  24. {
  25. register double r,p;
  26. register unsigned int x;
  27. register int r_,x_;
  28. const char *_s;
  29.  
  30. r = 0; r_ = 0;
  31.  
  32. if (!s) return(r);
  33.  
  34. while (isspace(*s++)); s--;
  35.  
  36. if (*s == '-' || *s == '+')
  37.   {
  38.   if (*s == '-') r_ = 1;
  39.   s++;
  40.   }
  41.  
  42. while (isdigit(*s))
  43.   {
  44.   r = r * 10 + (*s - '0');
  45.   s++;
  46.   }
  47.  
  48. if (*s != '.') goto integer;
  49.  
  50. s++; p = 1; while (isdigit(*s))
  51.   {
  52.   r = r * 10 + (*s - '0');
  53.   p *= 10;
  54.   s++;
  55.   }
  56. r /= p;
  57.  
  58. integer: if (!(*s == 'e' || *s == 'E')) goto ret;
  59.  
  60. x_ = (int)strtol(++s,(char **)&_s,0); s = _s;
  61.  
  62. x_ =  (x_ < 0) ? ((x = (unsigned int)(-x_)),1) : ((x = (unsigned int)x_),0);
  63.  
  64. if (x > MAXEXP) { r_ = 0; r = 0; goto ret; }
  65.  
  66. p = 1; while (x) p *= 10,x--; r = (x_) ? (r / p) : (r * p);
  67.  
  68. ret: if (end) *end = (char *)s;
  69.  
  70. return(r_ ? -r : r);
  71. }
  72.